package com.swq.java.code.chapter10.code04;

public class Sample {
    private static final String CLASS_NAME = Sample.class.getName();
    public static void main(String[] args) {
        System.out.println("class name : " + CLASS_NAME);
        Class c;
        ClassLoader cl, cl1;
        cl = ClassLoader.getSystemClassLoader();  //获得系统加载器
        System.out.println(cl);  //打印系统加载器
        while (cl != null) {     //打印父加载器
            cl1 = cl;
            cl = cl.getParent();
            System.out.println(cl1 + "'s parent is " + cl);
        }

        try {
            c = Class.forName("java.lang.Object");  //获得代表Object类的Class实例
            cl = c.getClassLoader();  //获得加载Object类的加载器
            // 用null表示根类加载器，不提供引用保证Java虚拟机安全
            System.out.println("java.lang.Object's loader is " + cl);

            c = Class.forName(CLASS_NAME);  //获得代表Sample类的Class实例
            cl = c.getClassLoader();   //获得加载Sample类的加载器
            System.out.println("Sample's loader is " + cl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}